package sw6.app5;

import java.util.ArrayList;

import sw6.lib.exceptions.SettingNotFoundException;
import sw6.lib.internal.debug.*;
import sw6.lib.Settings;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

/**
 * Tests install of sw6.app5v2
 * Version sw6.app5v1 did contain a
 * settings.xml file, while this upgraded
 * version do not. So, this test will check
 * if the upgrade procedure works as intended:
 * that is, if it sees that a settings.xml file
 * is missing, and then removes all settings
 * regarding sw6.app5 from the database.
 * @author sw6b
 */
public class TestActivity extends Activity {
	
	private static final String APP_NAME 	= "sw6.app5v2";
	private int testNumber 					= 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//Define the resulting intent
		Intent resultIntent 		= new Intent();
		ArrayList<Boolean> results 	= new ArrayList<Boolean>();
		
		// TEST NO. 0
		boolean hasConcludedTest0 = false;
		try {
			Settings.getBoolean(this, "boolean1");
		} catch(SettingNotFoundException e) {
			hasConcludedTest0 = true;
			DebugHelpers.logPass(testNumber, APP_NAME);
			results.add(true);
		} finally {
			if(!hasConcludedTest0) {
				DebugHelpers.logIssue(testNumber, APP_NAME, "Expected: an exception of type sw6.lib.exceptions.SettingNotFoundException");
				results.add(false);
			}
		}
		testNumber++;
		
		
		// TEST NO. 1
		boolean hasConcludedTest1 = false;
		try {
			Settings.getDouble(this, "double1");
		} catch(SettingNotFoundException e) {
			hasConcludedTest1 = true;
			DebugHelpers.logPass(testNumber, APP_NAME);
			results.add(true);
		} finally {
			if(!hasConcludedTest1) {
				DebugHelpers.logIssue(testNumber, APP_NAME, "Expected: an exception of type sw6.lib.exceptions.SettingNotFoundException");
				results.add(false);
			}
		}
		testNumber++;
		
		// TEST NO. 2
		boolean hasConcludedTest2 = false;
		try {
			Settings.getInteger(this, "int1");
		} catch(SettingNotFoundException e) {
			hasConcludedTest2 = true;
			DebugHelpers.logPass(testNumber, APP_NAME);
			results.add(true);
		} finally {
			if(!hasConcludedTest2) {
				DebugHelpers.logIssue(testNumber, APP_NAME, "Expected: an exception of type sw6.lib.exceptions.SettingNotFoundException");
				results.add(false);
			}
		}
		testNumber++;
		

		// TEST NO. 3
		boolean hasConcludedTest3 = false;
		try {
			Settings.getInteger(this, "string1");
		} catch(SettingNotFoundException e) {
			hasConcludedTest3 = true;
			DebugHelpers.logPass(testNumber, APP_NAME);
			results.add(true);
		} finally {
			if(!hasConcludedTest3) {
				DebugHelpers.logIssue(testNumber, APP_NAME, "Expected: an exception of type sw6.lib.exceptions.SettingNotFoundException");
				results.add(false);
			}
		}
		testNumber++;
		
		// TEST NO. 4
		boolean hasConcludedTest4 = false;
		try {
			Settings.getInteger(this, "enum1");
		} catch(SettingNotFoundException e) {
			hasConcludedTest4 = true;
			DebugHelpers.logPass(testNumber, APP_NAME);
			results.add(true);
		} finally {
			if(!hasConcludedTest4) {
				DebugHelpers.logIssue(testNumber, APP_NAME, "Expected: an exception of type sw6.lib.exceptions.SettingNotFoundException");
				results.add(false);
			}
		}
		testNumber++;
		
		// TEST NO. 5
		boolean hasConcludedTest5 = false;
		try {
			Settings.getInteger(this, "object1");
		} catch(SettingNotFoundException e) {
			hasConcludedTest5 = true;
			DebugHelpers.logPass(testNumber, APP_NAME);
			results.add(true);
		} finally {
			if(!hasConcludedTest5) {
				DebugHelpers.logIssue(testNumber, APP_NAME, "Expected: an exception of type sw6.lib.exceptions.SettingNotFoundException");
				results.add(false);
			}
		}
		
		resultIntent.putExtra("appName", this.getString(R.string.app_name));
		resultIntent.putExtra("results", DebugHelpers.getBooleanArray(results));
		setResult(RESULT_OK, resultIntent);
		finish();
	}
}
